/*
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hd.assistant.web.home.module.screen.sxj;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.hd.assistant.biz.service.exception.ServiceException;
import com.hd.assistant.biz.util.PaipaiUtil;
import com.hd.assistant.dal.dataobject.User;

/**
*
* @author Hangding
*/
public class PaipaiHome extends BasePaipaiSxjModule{
    private final static Logger log = LoggerFactory.getLogger(PaipaiHome.class);
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private HttpSession session;
    
   
    public void execute(
    		@Param("useruin") Long userUin,
    		@Param("access_token") String accessToken,
    		@Param("app_oauth_id") String appOauthId,
    		@Param("sign") String sign,
    		Context context, Navigator nav) {
    	try{
	    	if(userUin == null || userUin == 0 || accessToken==null){
	    		nav.redirectToLocation(PaipaiUtil.getRedirectLoginUrl(getCurrentAppId()));
	    	}else{
	    		User user = checkLogin(session);
	    		if(user != null && user.getUserId().equals(userUin)){
	    			supplyUserInfo(user,context);
	    			context.put("user", user);
	    		}else{
	    			User u = getUserInfo(userUin);
	    			if(u == null)
	    			{
	    				u = new User();
	    				u.setUserType(getCurrentUserApp());
	    				u.setUserId(userUin);
	    				u.setCurrentToken(accessToken);
	    				u.setCurrentOAuthId(appOauthId);
	    				supplyUserInfo(u,context);
	    				storeUserInfo(u);
	    			}else{
	    				u.setCurrentToken(accessToken);
	    				u.setCurrentOAuthId(appOauthId);
	    				updateUserInfo(u);
	    			}
	    			context.put("user", u);
	    			session.setAttribute(PaipaiUtil.cookieMap.get(getCurrentAppId()), u);
		    		
	    		}
	    		
	    	}
    	}catch(ServiceException se){
    		log.error("error:",se);
    		context.put("error", userUin+" "+se.getDisplayError());
    		if(se.needReLogin){
    			nav.redirectToLocation(PaipaiUtil.getRedirectLoginUrl(PaipaiUtil.APP_OAUTH_ID_ASSISTANT));
    		}else{
    			nav.forwardTo("error");
    		}
    		
    	}catch(Exception e){
    		log.error("error:",e);
    		context.put("error", e.getMessage());
    		nav.forwardTo("error");
    	}
    }	

	
}
